package com.github.ghsea.sso.client.config;

import com.google.common.base.Preconditions;
import java.util.Set;
import org.springframework.beans.factory.InitializingBean;

/**
 * 给接入方应用定制shiro相关履性
 *
 * @author guhai
 */
public class ShiroConfig implements InitializingBean {

    private String casServerUrl;

    /**
     * 例如，对于以下URL，应用的上下文是bams，applicationPath为/a
     * http://oldoperation.xxxx.com/bams/a
     */
    private String applicationPath = "";

    private String applicationDomain;

    /**
     * web应用上下文
     */
    private String servletContextPath;

    private String sessionKey;

    /**
     * 会话超时， 单位：毫秒.1 小时
     */
    private long sessionTimeoutMs = 1000 * 3600;

    /**
     * 会话清理间隔时间， 单位：毫秒，2m=120000ms。
     */
    private long sessionTimeoutCleanMs = 120000;

    /**
     * 查询用户的http url
     */
    private String apiUrlGetUser;

    /**
     * 查询权限的http url
     */
    private String apiUrlGetPermissions;

    private String cookieDomain;

    private String redisHost;

    private int redisPort;

    private String redisPassword;

    /**
     * 无须登录就可访问的path
     */
    private Set<String> pathsWithoutLogin;

    private boolean httpOnly = false;

    private final String CAS_URL = "/cas";

    private boolean https = false;

    public String getCasServerUrl() {
        return casServerUrl;
    }

    public ShiroConfig setCasServerUrl(String casServerUrl) {
        this.casServerUrl = casServerUrl;
        return this;
    }

    public String getApplicationPath() {
        return applicationPath;
    }

    public ShiroConfig setApplicationPath(String applicationPath) {
        this.applicationPath = applicationPath;
        return this;
    }

    public long getSessionTimeoutMs() {
        return sessionTimeoutMs;
    }

    public ShiroConfig setSessionTimeoutMs(long sessionTimeoutMs) {
        this.sessionTimeoutMs = sessionTimeoutMs;
        return this;
    }

    public String getSessionKey() {
        return sessionKey;
    }

    public ShiroConfig setSessionKey(String sessionKey) {
        this.sessionKey = sessionKey;
        return this;
    }

    public long getSessionTimeoutCleanMs() {
        return sessionTimeoutCleanMs;
    }

    public ShiroConfig setSessionTimeoutCleanMs(long sessionTimeoutCleanMs) {
        this.sessionTimeoutCleanMs = sessionTimeoutCleanMs;
        return this;
    }

    public String getApiUrlGetUser() {
        return apiUrlGetUser;
    }

    public ShiroConfig setApiUrlGetUser(String apiUrlGetUser) {
        this.apiUrlGetUser = apiUrlGetUser;
        return this;
    }

    public String getApiUrlGetPermissions() {
        return apiUrlGetPermissions;
    }

    public ShiroConfig setApiUrlGetPermissions(String apiUrlGetPermissions) {
        this.apiUrlGetPermissions = apiUrlGetPermissions;
        return this;
    }

    public String getCookieDomain() {
        return cookieDomain;
    }

    public ShiroConfig setCookieDomain(String cookieDomain) {
        this.cookieDomain = cookieDomain;
        return this;
    }

    public String getRedisHost() {
        return redisHost;
    }

    public ShiroConfig setRedisHost(String redisHost) {
        this.redisHost = redisHost;
        return this;
    }

    public int getRedisPort() {
        return redisPort;
    }

    public ShiroConfig setRedisPort(int redisPort) {
        this.redisPort = redisPort;
        return this;
    }

    public String getRedisPassword() {
        return redisPassword;
    }

    public ShiroConfig setRedisPassword(String redisPassword) {
        this.redisPassword = redisPassword;
        return this;
    }

    public String getServletContextPath() {
        return servletContextPath;
    }

    public ShiroConfig setServletContextPath(String servletContextPath) {
        this.servletContextPath = servletContextPath;
        return this;
    }

    public String getApplicationDomain() {
        return applicationDomain;
    }

    public ShiroConfig setApplicationDomain(String applicationDomain) {
        this.applicationDomain = applicationDomain;
        return this;
    }

    public Set<String> getPathsWithoutLogin() {
        return pathsWithoutLogin;
    }

    public ShiroConfig setPathsWithoutLogin(Set<String> pathsWithoutLogin) {
        this.pathsWithoutLogin = pathsWithoutLogin;
        return this;
    }

    public boolean isHttpOnly() {
        return httpOnly;
    }

    public ShiroConfig setHttpOnly(boolean httpOnly) {
        this.httpOnly = httpOnly;
        return this;
    }

    public boolean isHttps() {
        return https;
    }

    public ShiroConfig setHttps(boolean https) {
        this.https = https;
        return this;
    }

    @Override
    public void afterPropertiesSet() {
        if (sessionKey == null) {
            sessionKey = "backend-sso";
        }

        if (servletContextPath == null) {
            servletContextPath = "/";
        }


        Preconditions.checkArgument(applicationDomain != null);
        if (!applicationDomain.startsWith("http") && !applicationDomain.startsWith("https")) {
            applicationDomain = "http://" + applicationDomain;
        }

        Preconditions.checkArgument(cookieDomain != null);
        Preconditions.checkArgument(casServerUrl != null);
        Preconditions.checkArgument(apiUrlGetUser != null);
        Preconditions.checkArgument(apiUrlGetPermissions != null);
    }

    public String computeCasLoginUrl() {
        String shrioApplicationUrl = applicationDomain + servletContextPath;
        return casServerUrl + "/login?service=" + shrioApplicationUrl + CAS_URL;
    }

    public String computeShirCasServiceUrl() {
        String shrioApplicationUrl = applicationDomain + servletContextPath;
        return shrioApplicationUrl + CAS_URL;

    }

}
